//TestScriptBlock.block
importPackage(com.blocks.blocks);
importPackage(com.blocks);
importPackage(com.blocks.model);
importPackage(com.jme3.math);
importPackage(java.lang);
importPackage(com.blocks.script);
importPackage(com.jme3.math);
importPackage(com.jme3.collision);
importPackage(com.jme3.scene.debug);
importPackage(com.jme3.scene);
importPackage(com.jme3.material);

var block = new Object();
var counter = 0;

var direction = 0;
var rayHit = false;
var markInput1;
var markInput2;
block.setup = function(name) {
	thisBlock.setName("NOR");
	thisBlock.setTextureRow(2.0);
	thisBlock.setTextureColumn(2.0);
	thisBlock.resetTexture();
	// thisBlock.setCustomFrequency(true);
	thisBlock.getParameters().put("axis", 2);

}
block.loop = function(name, tpf) {
	var found = false;
	var axis = new Float(thisBlock.getParameters().get("axis")).intValue();
	if (thisBlock.isPowered()) {

		switch (axis) {
		case 0:
			thisBlock.setColor(ColorRGBA.Red);
			break;
		case 1:
			thisBlock.setColor(ColorRGBA.Orange);
			break;
		case 2:
			thisBlock.setColor(ColorRGBA.Yellow);
			break;
		case 3:
			thisBlock.setColor(ColorRGBA.Green);
			break;
		case 4:
			thisBlock.setColor(ColorRGBA.Blue);
			break;
		default:
			thisBlock.setColor(ColorRGBA.Magenta);
			break;
		}

		masterBlock = ScriptBlocksApplication.getInstance().getMasterBlock(
				thisBlock.getMasterId());

		var input1 = block.collision((axis + 1) % 6);
		var input2 = block.collision((axis + 4) % 6);
		if(input1>0 || input2>0){
			thisBlock.setAllFrequency(0);
		}else {
			thisBlock.setAllFrequency(1);
		}
		

	}
	if (!found) {
		if (markInput1 != null) {
			thisBlock.detachChild(markInput1);
		}
		if (markInput2 != null) {
			thisBlock.detachChild(markInput2);
		}		
		markInput1 = block.initMark((axis + 1) % 6, ColorRGBA.Blue);
		markInput2 = block.initMark((axis + 4) % 6, ColorRGBA.Blue);
		thisBlock.attachChild(markInput1);
		thisBlock.attachChild(markInput2);
	}
}

block.use = function() {
	var axis = new Float(thisBlock.getParameters().get("axis")).intValue();
	axis = (axis + 1) % 6;
	thisBlock.getParameters().put("axis", axis);
}

block.scriptExport = function() {
	return "";
}

block.initMark = function(axis, color, input) {

	var arrow = new Arrow(Vector3f.UNIT_X.mult(2));
	if (axis == 1) {
		arrow = new Arrow(Vector3f.UNIT_Y.mult(2));
	} else if (axis == 2) {
		arrow = new Arrow(Vector3f.UNIT_Z.mult(2));
	} else if (axis == 3) {
		arrow = new Arrow(Vector3f.UNIT_X.mult(-2));
	} else if (axis == 4) {
		arrow = new Arrow(Vector3f.UNIT_Y.mult(-2));
	} else if (axis == 5) {
		arrow = new Arrow(Vector3f.UNIT_Z.mult(-2));
	}
	arrow.setLineWidth(3);

	var mark = new Geometry("BOOM!", arrow);

	mark_mat = new Material(ScriptBlocksApplication.getInstance()
			.getAssetManager(), "Common/MatDefs/Misc/SolidColor.j3md");
	mark_mat.setColor("Color", color);
	mark.setMaterial(mark_mat);

	return mark;
}

block.collision = function(axis) {
	var results = new CollisionResults();
	var column = axis % 3;
	direction = thisBlock.getWorldRotation().getRotationColumn(column);
	if (axis > 2) {
		direction = direction.mult(-1);
	}
	ray = new Ray(thisBlock.getWorldTranslation(), direction);
	ScriptBlocksApplication.getInstance().getRootNode().collideWith(ray,
			results);

	var distance = 0;
	if (results.size() > 0) {
		itr = results.iterator();
		while (itr.hasNext()) {
			closest = itr.next();

			if (closest.getGeometry() != null && closest.getDistance() > 1
					&& closest.getDistance() < 20) {
				hitNode = closest.getGeometry().getParent();
				if (ScriptUtils.isBlock(hitNode)) {
					var triangle = closest.getTriangleIndex();
					var focuseBlockSides = BlockSides.getBlockSideFromTriangle(triangle);
					var returnValue = hitNode.getFrequency(focuseBlockSides);
					//println(axis + " RETURN VALUE " + returnValue)
					return returnValue;
					
				}
			}
		}
	}
	return 0;

}
